import logging
import os
import webapp2

from db.stats_client import StatsClient
from google.appengine.api import users
from google.appengine.ext.webapp import template


class EmailStatsPage(webapp2.RequestHandler):
  def get(self):
    stats_client = StatsClient()
    user = users.get_current_user()
    student_map = stats_client.getStudentMap(session='2012-01')

    students_completed = []
    students_in_progress = []
    students_not_started = []

    for student in student_map.values():
      if student.status == 'completed':
        students_completed.append(student)
      elif student.status:
        students_in_progress.append(student)
      else:
        students_not_started.append(student)

    tmpl_data = {
      'students_completed': students_completed,
      'students_in_progress': students_in_progress,
      'students_not_started': students_not_started
    }
    path = os.path.join(os.path.dirname(__file__), 'emailstats.html')
    self.response.out.write(template.render(path, tmpl_data))


app = webapp2.WSGIApplication([('/emailstats', EmailStatsPage)], debug=True)
